﻿var express = require('express');
var router = express.Router();
var connection = require('../backjs/node_mysql.js')
var session = require('express-session');

router.use(session({
    resave: false, //添加 resave 选项
    saveUninitialized: true, //添加 saveUninitialized 选项
    secret: 'aF,.j)wBhq+E9n#aHHZ91Ba!VaoMfC', // 建议使用 128 个字符的随机字符串
    cookie: { maxAge: 20 * 1000 * 60 }  //ms
}));

//公布评片记录
router.post('/photo_disclose', function (req, res, next) {
    var sql = 'update photo set ph_power = 1 where ph_id in (?)';
    var params = [req.body.ph_ids];
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err;
        }
        else {
            res.send(200);
        }
    });
});

//客户查看评片记录
router.post('/photo_check', function (req, res, next) {
    var sql = 'select ph_id,w_pipeNum,w_weldNum,w_weldPer,w_spec,ph_phNum,ph_spec,ph_IQISen,ph_bmin,ph_bmax,ph_flaw,ph_pos,ph_size, ' +
        'ph_evalevel,ph_conclusion from photo,workpiece where ph_client = ? and ph_power = 1 and ph_wid = w_id ';
    var params = req.body.ph_ids;
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err;
        }
        else {
            res.send(rs);
        }
    });
});

//报告编制和查看
router.post('/report', function (req, res, next) {
    //生成报告的工件列表
    if (req.body.requestType == 'worklist') {
        var sql = 'select task_pname,task_pid,w_tid,w_id,task_product,task_pdid,w_pipeName,w_pipeNum,w_weldNum,w_weldPer,w_spec,w_re,task_company,w_test, ' +
            'w_qua,w_scale,w_texture,w_weldM,d_fin,op_id,op_techid from task,workpiece,dispatch,operation where task_department = ? and task_id = w_tid and ' +
            'w_did = d_id and d_opid = op_id and w_rstate = 0';
        var params = [req.body.user_part];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    if (req.body.requestType == 'sessionData') {
        req.session.pj_id = req.body.pj_id;
        req.session.task_ids = req.body.task_ids;
        req.session.w_ids = req.body.w_ids;
        req.session.op_ids = req.body.op_ids;
        req.session.tech_ids = req.body.tech_ids;
        req.session.task_product = req.body.task_product;
        res.send(200);
    }
    //报告基本信息预设
    if (req.body.requestType == 'crudeBaseData') {
        var sql = 'select distinct pj_name from project where pj_id in (?);' +
            'select distinct pj_company from project where pj_id in (?);' +
            'select distinct task_position from task where task_id in (?);' +
            'select distinct task_opportunity from task where task_id in (?);' +
            'select distinct op_weldM from operation where op_id in (?);' +//op_id
            'select distinct w_type from workpiece where w_id in (?);' +//w_id 坡口形式
            'select distinct tech_focus from tech where tech_id in (?);' +//w_techid 设备型号
            'select distinct op_sensitizer from operation where op_id in (?);' +//增感方式
            'select distinct op_filmBrand from operation where op_id in (?);' +
            'select distinct op_filmV from operation where op_id in (?);' +
            'select distinct tech_devTime from tech where tech_id in (?);' +//显影时间
            'select distinct tech_devCondition from tech where tech_id in (?);' +
            'select distinct task_standard from task where task_id in (?);' +
            'select distinct w_qua from workpiece where w_id in (?);' +//合格级别
            'select distinct task_heat from task where task_id in (?);' +//热处理状态
            'select distinct op_texture from operation where op_id in (?);' +//材质
            'select distinct op_equid from operation where op_id in (?);' +//设备编号
            'select distinct op_liquor from operation where op_id in (?);' +//显影液配方
            'select distinct tech_devT from tech where tech_id in (?);' +//显影温度
            'select distinct task_level from task where task_id in (?);' +
            'select distinct task_check from task where task_id in (?);' +
            'select distinct op_scale from operation where op_id in (?);' +
            'select distinct op_equ from operation where op_id in (?);' +
            'select distinct tech_IQIV from tech where tech_id in (?);' +

            'select min(ph_bmin) as min from photo where ph_wid in (?);' +
            'select max(ph_bmax) as max from photo where ph_wid in (?);'+
            'select distinct task_product from task where task_id in (?);' ;
        var params = [req.session.pj_id, req.session.pj_id, req.session.task_ids, req.session.task_ids, req.session.op_ids, req.session.w_ids, req.session.tech_ids,
        req.session.op_ids, req.session.op_ids, req.session.op_ids, req.session.tech_ids, req.session.tech_ids, req.session.task_ids, req.session.w_ids,
        req.session.task_ids, req.session.op_ids, req.session.op_ids, req.session.op_ids, req.session.tech_ids, req.session.task_ids, req.session.task_ids, req.session.op_ids,
        req.session.op_ids, req.session.tech_ids,req.session.w_ids, req.session.w_ids, req.session.task_ids];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    //报告工艺信息查看
    if (req.body.requestType == 'techData') {
        var sql = 'select w_V,w_A,tech_id,tech_version,tech_tranType,tech_IQISen,tech_focalDis,tech_wcid,tech_tranLen,tech_seg,tech_time,' +
            'w_spec,SUM(w_phoNum) as sum from (select tech_time,tech_id,tech_version,tech_tranType,tech_IQISen,tech_focalDis,tech_wcid,tech_tranLen,' +
            'tech_seg from tech where tech_id in (?))tmp inner join workpiece on w_wcid = tmp.tech_wcid group by tech_id,' +
            'tech_version,tech_tranType,tech_IQISen,tech_focalDis,tech_wcid,tech_tranLen,tech_seg,w_V,w_A,w_spec,tech_time';
        var params = [req.session.tech_ids];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    //拍片信息查看
    if (req.body.requestType == 'photoData') {
        var sql = 'select photo.*,w_pipeNum,w_weldNum,w_re from photo,workpiece where w_id in (?) and w_id = ph_wid'
        var params = [req.session.w_ids];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send({
                    "photo": rs,
                    "w_num": req.session.w_ids.length
                });
            }
        });
    }
    //保存报告基本信息
    if (req.body.requestType == 'update') {
        var sql = 'insert into report (r_pid,r_pname,r_company,r_position,r_opportunity,r_weldM,r_type, r_equ,r_focus,r_IQIV,r_sensitizer,r_filmBrand,r_state,' +
            'r_filmV,r_devTime,r_devCondition,r_b,r_standard,r_qua,r_heat,r_texture,r_equid,r_develop,r_devT,r_tlevel,r_check,r_scale,r_person,r_level,r_date,r_part,r_remark,r_product) values (?)';
        var params = [[req.session.pj_id[0], req.body.r_pname, req.body.r_company, req.body.r_position, req.body.r_opportunity,
        req.body.r_weldM, req.body.r_type, req.body.r_equ, req.body.r_focus, req.body.r_IQIV, req.body.r_sensitizer, req.body.r_filmBrand, 0,
        req.body.r_filmV, req.body.r_devTime, req.body.r_devCondition, req.body.r_b, req.body.r_standard, req.body.r_qua,
        req.body.r_heat, req.body.r_texture, req.body.r_equid, req.body.r_develop, req.body.r_devT, req.body.r_tlevel,
        req.body.r_check, req.body.r_scale, req.body.r_person, req.body.r_level, req.body.r_date, req.body.r_part, req.body.r_remark,req.body.r_product]];
        var rId;
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                rId = rs.insertId;
                //保存报告拥有的工件信息
                sql = 'insert into workrep (wr_rid,wr_wid) values ?';
                var values = [];
                for (var i = 0; i < req.session.w_ids.length; i++) {
                    if (req.session.w_ids[i] == '0'){
                        continue
                    }
                    var vec = [rId, req.session.w_ids[i]];
                    values.push(vec);
                }
                console.log(values);
                connection.query(sql, [values], function (err, rs) {
                    if (err) {
                        throw err;
                    }
                    else {
                        console.log('aaaa')
                         // sql1 = 'UPDATE work SET myfield = \'value\' WHERE other_field in (\'other_values\');
                         var sql = 'update workpiece set w_rstate = 1 where w_id in (?)'
                         var params =  [req.session.w_ids];
                         connection.query(sql,params, function (err, rs) {
                             if (err) {
                                 throw err;
                             }
                             else {
                        res.send(200);
                         }
                         })
                        // res.send(200)
                    }
                });
            }
        });
    }
    //报告基本信息查看
    if (req.body.requestType == 'baseData') {
        var sql = 'select * from report where report_id = ?';
        var params = [req.body.r_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    //读取报告的工件id和对应工艺卡id
    if (req.body.requestType == 'workpieceData') {
        var sql = 'select wr_wid,tech_id from workrep,tech,workpiece where wr_rid = ? and wr_wid = w_id and tech_wcid = w_wcid'
        var params = [req.body.r_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                var sql0 = 'select report.*,a.user_name as m_name,b.user_name as v_name,c.user_name as a_name from report left join user a on r_person = a.user_id ' +
                    'left join user b on r_verify_person = b.user_id left join user c on r_approve_person = c.user_id where r_id = ?';
                var params0 = [req.body.r_id];
                connection.query(sql0, params0, function (err0, rs0) {
                    if (err0) {
                        throw err0;
                    }
                    else {
                        var sql1 = 'select photo.*,w_pipeNum,w_weldNum,w_re from photo,workpiece where w_id in (?) and w_id = ph_wid'
                        let w_ids = [];
                        let tech_ids = [];
                        for (let i = 0; i < rs.length; i++) {
                            w_ids[i] = rs[i].wr_wid;
                            tech_ids[i] = rs[i].tech_id;
                        }
                        var params1 = [w_ids];
                        connection.query(sql1, params1, function (err1, rs1) {
                            if (err1) {
                                throw err1;
                            }
                            else {
                                var sql2 = 'select w_V,w_A,tech_id,tech_version,tech_tranType,tech_IQISen,tech_focalDis,tech_wcid,tech_tranLen,tech_seg,tech_time,' +
                                    'w_spec,SUM(w_phoNum) as sum from (select tech_time,tech_id,tech_version,tech_tranType,tech_IQISen,tech_focalDis,tech_wcid,tech_tranLen,' +
                                    'tech_seg from tech where tech_id in (?))tmp inner join workpiece on w_wcid = tmp.tech_wcid group by tech_id,' +
                                    'tech_version,tech_tranType,tech_IQISen,tech_focalDis,tech_wcid,tech_tranLen,tech_seg,w_V,w_A,w_spec,tech_time';
                                var params2 = [tech_ids];
                                connection.query(sql2, params2, function (err2, rs2) {
                                    if (err2) {
                                        throw err2;
                                    }
                                    else {
                                        res.send({
                                            "photo": rs1,
                                            "w_num": rs.length,
                                            "base": rs0,
                                            "tech": rs2
                                        });
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });
    }
    if(req.body.requestType == 'changeState'){
        sql = 'UPDATE report set r_state = 5 where r_id = ?'
        var params = [req.body.r_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});

//客户报告列表
router.post('/report_client', function (req, res, next) {
    var sql = 'select report.* from report,project where r_pid = pj_id and pj_client = ? and r_state = 5 order by r_id DESC';
    var params = [req.body.user_id];
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err;
        }
        else {
            console.log(rs);
            res.send(rs);
        }
    });
});

//报告已编制列表
router.post('/report_list', function (req, res, next) {
    var sql = 'select report.*,project.pj_client,user.user_name from report,project,user where r_part = ? and r_pid = pj_id and pj_client = user_id order by r_id DESC';
    var params = [req.body.user_part];
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err;
        }
        else {
            res.send(rs);
        }
    });
});

//报告审核
router.post('/report_check', function (req, res, next) {
    if (req.body.requestType == 'check') {
        var sql = 'insert into report (r_id,r_state,r_verify_person,r_verify_level,r_verify_date,r_reason) values ? ON DUPLICATE KEY UPDATE ' +
            'r_state=values(r_state),r_verify_person=values(r_verify_person),r_verify_level=values(r_verify_level),r_verify_date=values(r_verify_date),r_reason=values(r_reason)'
        var params = [[req.body.r_id, req.body.r_state, req.body.r_verify_person, req.body.r_verify_level, req.body.r_verify_date, req.body.r_reason]];
        connection.query(sql, [params], function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    if (req.body.requestType == 'list') {
        var sql = 'select * from report order by r_id DESC';
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});

//报告发送给用户
router.post('/report_public', function (req, res, next) {
    if (req.body.requestType == 'check') {
        var sql = 'insert into report (r_id,r_state,r_approve_person,r_approve_level,r_approve_date,r_reason) values ? ON DUPLICATE KEY UPDATE ' +
            'r_state=values(r_state),r_approve_person=values(r_approve_person),r_approve_level=values(r_approve_level),r_approve_date=values(r_approve_date),r_reason=values(r_reason)'
        var params = [[req.body.r_id, req.body.r_state, req.body.r_approve_person, req.body.r_approve_level, req.body.r_approve_date, req.body.r_reason]];
        connection.query(sql, [params], function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    if (req.body.requestType == 'list') {
        var sql = 'select report.*,project.pj_client,user.user_name from report,project,user where r_state >= 4 ' +
            'and r_pid = pj_id and pj_client = user_id  order by r_id DESC';
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});

//超级管理员批准报告
router.post('/report_super_approve', function (req, res, next) {
    if (req.body.requestType == 'list') {
        var sql = 'select report.*,project.pj_client,user.user_name from report,project,user where r_state >= 2 and r_state <= 4 ' +
            'and r_pid = pj_id and pj_client = user_id order by r_id DESC ';
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }else if (req.body.requestType == 'check'){
        var sql = 'insert into report (r_id,r_state,r_approve_person,r_approve_level,r_approve_date,r_reason) values ? ON DUPLICATE KEY UPDATE ' +
            'r_state=values(r_state),r_approve_person=values(r_approve_person),r_approve_level=values(r_approve_level),r_approve_date=values(r_approve_date),r_reason=values(r_reason)'
        var params = [[req.body.r_id, req.body.r_state, req.body.r_approve_person, req.body.r_approve_level, req.body.r_approve_date, req.body.r_reason]];
        connection.query(sql, [params], function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }

});

//发送
router.post('/report_send', function (req, res, next) {
    if (req.body.requestType == 'check') {
        var sql = 'insert into report (r_id,r_state) values ? ON DUPLICATE KEY UPDATE ' +
            'r_state=values(r_state)'
        var params = [[req.body.r_id, req.body.r_state]];
        connection.query(sql, [params], function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    if (req.body.requestType == 'list') {
        var sql = 'select report.*,project.pj_client,user.user_name from report,project,user where r_state >= 4 ' +
            'and r_pid = pj_id and pj_client = user_id order by r_id DESC';
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                console.log(rs);
                res.send(rs);
            }
        });
    }
});
module.exports = router;